﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class FormPipingFittingSet : System.Windows.Forms.Form
	{
		public FormPipingFittingSet(Document doc, HVACDataReader hvacDateRead, string Message)
		{
			base.CenterToScreen();
			//base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.hvacDR = hvacDateRead;
			this.InitializeComponent();
			this.message = Message;
			if (this.message == "风")
			{
				this.nodeName = "ductFittingSetInfo";
			}
			if (this.message == "水")
			{
				this.nodeName = "pipeFittingSetInfo";
			}
			if (this.message == "风对齐")
			{
				this.nodeName = "ductAlignMentFittingSetInfo";
			}
			this.teeTop = this.getDuctAlighmentDbFamily("矩形三通", "顶");
			this.teebot = this.getDuctAlighmentDbFamily("矩形三通", "底");
			this.crossTop = this.getDuctAlighmentDbFamily("矩形四通", "顶");
			this.crossbot = this.getDuctAlighmentDbFamily("矩形四通", "底");
			this.teeMiddle = this.getDuctAlighmentDbFamily("矩形三通", "中心");
			this.crossMiddle = this.getDuctAlighmentDbFamily("矩形四通", "中心");
			string[] ductAlignmentPipingFittingText = this.userSetting.GetDuctAlignmentPipingFittingText();
			this.teeTopFamily = this.updateStartFamily(this.teeTop, ductAlignmentPipingFittingText[0]);
			this.teebotFamily = this.updateStartFamily(this.teebot, ductAlignmentPipingFittingText[1]);
			this.crossTopFamily = this.updateStartFamily(this.crossTop, ductAlignmentPipingFittingText[2]);
			this.crossbotFamily = this.updateStartFamily(this.crossbot, ductAlignmentPipingFittingText[3]);
			this.teeMiddleFamily = this.updateStartFamily(this.teeMiddle, ductAlignmentPipingFittingText[4]);
			this.crossMiddleFamily = this.updateStartFamily(this.crossMiddle, ductAlignmentPipingFittingText[5]);
		}

		private List<FittingInfo> readUserLastSet()
		{
			List<FittingInfo> list = new List<FittingInfo>();
			List<string[]> list2 = new List<string[]>();
			try
			{
				list2 = this.userSetting.ReadPipingFittingSetInfo("ductFittingSet");
				if (list2 != null)
				{
					foreach (string[] array in list2)
					{
						list.Add(new FittingInfo
						{
							systemClass = array[0],
							fittingName = array[1]
						});
					}
				}
			}
			catch
			{
			}
			return list;
		}

		private List<DbFamily> getDuctAlighmentDbFamily(string mess, string alignment)
		{
			List<DbFamily> list = new List<DbFamily>();
			foreach (DbFamily dbFamily in this.hvacDR.GetFamilyAllInfoList(mess))
			{
				if (alignment != "中心")
				{
					if (dbFamily.Name.Contains(alignment))
					{
						list.Add(dbFamily);
					}
				}
				else if (!dbFamily.Name.Contains("顶") && !dbFamily.Name.Contains("底"))
				{
					list.Add(dbFamily);
				}
			}
			return list;
		}

		private DbFamily updateStartFamily(List<DbFamily> lsasd, string naa)
		{
			DbFamily dbFamily = null;
			foreach (DbFamily dbFamily2 in lsasd)
			{
				if (dbFamily2.Name == naa)
				{
					dbFamily = dbFamily2;
					break;
				}
			}
			if (dbFamily == null)
			{
				dbFamily = lsasd.ElementAt(0);
			}
			return dbFamily;
		}

		private void FormPipingFittingSet_Load(object sender, EventArgs e)
		{
			this.listView1.LargeImageList.ImageSize = new Size(70, 70);
			this.listView1.MultiSelect = false;
			this.listView1.HideSelection = true;
			if (this.message == "风对齐")
			{
				this.Text = "风管对齐连接管件设置";
				TreeNode treeNode = new TreeNode("管件");
				this.treeView1.Nodes.Add(treeNode);
				TreeNode treeNode2 = new TreeNode("矩形三通");
				TreeNode treeNode3 = new TreeNode("顶对齐");
				TreeNode node = new TreeNode("底对齐");
				TreeNode node2 = new TreeNode("中心对齐");
				TreeNode treeNode4 = new TreeNode("矩形四通");
				TreeNode node3 = new TreeNode("顶对齐");
				TreeNode node4 = new TreeNode("底对齐");
				TreeNode node5 = new TreeNode("中心对齐");
				treeNode.Nodes.Add(treeNode2);
				treeNode.Nodes.Add(treeNode4);
				treeNode2.Nodes.Add(treeNode3);
				treeNode2.Nodes.Add(node2);
				treeNode2.Nodes.Add(node);
				treeNode4.Nodes.Add(node3);
				treeNode4.Nodes.Add(node5);
				treeNode4.Nodes.Add(node4);
				this.isload1 = true;
				this.treeView1.SelectedNode = treeNode3;
				this.tnSelected = treeNode3;
				this.treeView1.ExpandAll();
				this.listViewIninit(this.teeTop, this.teeTopFamily);
				return;
			}
			this.listView1.MultiSelect = false;
			if (this.hvacDR != null)
			{
				if (this.message == "风")
				{
					this.hvacDR.GetDuctFittingSetFamilyAllInfoTree("管件", "风系统", this.treeView1);
				}
				else
				{
					this.hvacDR.GetDuctFittingSetFamilyAllInfoTree("管件", "水系统", this.treeView1);
				}
			}
			foreach (TreeNode treeNode5 in TreeViewTools.GetAllChildNode(this.treeView1.Nodes[0]))
			{
				if (treeNode5.Nodes.Count < 1)
				{
					treeNode5.Remove();
				}
			}
			this.treeView1.ExpandAll();
			this.treeView1.SelectedNode = this.treeView1.Nodes[0].FirstNode;
			this.structList = this.readUserLastSet();
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			if (this.message == "风对齐")
			{
				string[] texts = new string[]
				{
					this.teeTopFamily.Name,
					this.teebotFamily.Name,
					this.crossTopFamily.Name,
					this.crossbotFamily.Name,
					this.teeMiddleFamily.Name,
					this.crossMiddleFamily.Name
				};
				this.userSetting.WriteDuctAlignmentPipingFittingText(texts);
				base.Close();
				return;
			}
			this.writeUserInfo(this.structList, this.nodeName);
		}

		private void writeUserInfo(List<FittingInfo> lll, string nodeName)
		{
			List<string[]> list = new List<string[]>();
			foreach (FittingInfo fittingInfo in this.structList)
			{
				list.Add(new string[]
				{
					fittingInfo.systemClass,
					fittingInfo.fittingName
				});
			}
			this.userSetting.WritePipingFittingSetInfo(nodeName, list);
		}

		private void listViewIninit(List<DbFamily> lll, DbFamily ddd)
		{
			this.listView1.Items.Clear();
			foreach (DbFamily dbFamily in lll)
			{
				this.listView1.LargeImageList.Images.Add(dbFamily.Name, dbFamily.Image);
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = dbFamily.Name;
				listViewItem.Tag = dbFamily;
				listViewItem.ImageKey = dbFamily.Name;
				this.listView1.Items.Add(listViewItem);
				listViewItem.EnsureVisible();
			}
			foreach (object obj in this.listView1.Items)
			{
				ListViewItem listViewItem2 = (ListViewItem)obj;
				if (listViewItem2.Text == ddd.Name)
				{
					listViewItem2.Selected = true;
					listViewItem2.EnsureVisible();
                    listViewItem2.BackColor = System.Drawing.Color.DodgerBlue;
                    listViewItem2.ForeColor = System.Drawing.Color.White;
				}
				else
				{
					listViewItem2.Selected = false;
                    listViewItem2.BackColor = System.Drawing.Color.White;
                    listViewItem2.ForeColor = System.Drawing.Color.Black;
				}
			}
		}

		private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.tnSelected = this.treeView1.SelectedNode;
			foreach (object obj in this.treeView1.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (!treeNode.IsSelected)
				{
                    treeNode.BackColor = System.Drawing.Color.White;
                    treeNode.ForeColor = System.Drawing.Color.Black;
				}
			}
			if (this.message == "风对齐")
			{
				this.isload2 = false;
				if (this.isload1)
				{
					if (e.Node.Level == 2)
					{
						if (e.Node.Parent.Text == "矩形三通")
						{
							if (e.Node.Text == "顶对齐")
							{
								this.listViewIninit(this.teeTop, this.teeTopFamily);
							}
							else if (e.Node.Text == "底对齐")
							{
								this.listViewIninit(this.teebot, this.teebotFamily);
							}
							else if (e.Node.Text == "中心对齐")
							{
								this.listViewIninit(this.teeMiddle, this.teeMiddleFamily);
							}
						}
						if (e.Node.Parent.Text == "矩形四通")
						{
							if (e.Node.Text == "顶对齐")
							{
								this.listViewIninit(this.crossTop, this.crossTopFamily);
							}
							else if (e.Node.Text == "底对齐")
							{
								this.listViewIninit(this.crossbot, this.crossbotFamily);
							}
							else if (e.Node.Text == "中心对齐")
							{
								this.listViewIninit(this.crossMiddle, this.crossMiddleFamily);
							}
						}
					}
					else if (e.Node.Level == 1)
					{
						this.treeView1.SelectedNode = e.Node.Nodes[0];
					}
					else if (e.Node.Level == 0)
					{
						this.treeView1.SelectedNode = e.Node.Nodes[0].Nodes[0];
					}
					this.isload2 = true;
					return;
				}
			}
			else
			{
				this.isLoad = false;
				if (e.Node.Nodes != null && e.Node.Nodes.Count < 2)
				{
					this.listView1.Items.Clear();
					foreach (DbFamily dbFamily in this.hvacDR.GetFamilyAllInfoList(e.Node.Name))
					{
						this.listView1.LargeImageList.Images.Add(dbFamily.Name, dbFamily.Image);
						ListViewItem listViewItem = new ListViewItem();
						listViewItem.Text = dbFamily.Name;
						listViewItem.Tag = dbFamily;
						listViewItem.ImageKey = dbFamily.Name;
						this.listView1.Items.Add(listViewItem);
						listViewItem.EnsureVisible();
					}
					IEnumerator enumerator = this.listView1.Items.GetEnumerator();
					{
						while (enumerator.MoveNext())
						{
							object obj2 = enumerator.Current;
							ListViewItem listViewItem2 = (ListViewItem)obj2;
							if (listViewItem2.Text == e.Node.Text)
							{
								listViewItem2.Selected = true;
							}
							else
							{
								listViewItem2.Selected = false;
							}
						}
						goto IL_3DF;
					}
				}
				if (e.Node == this.treeView1.Nodes[0])
				{
					this.treeView1.SelectedNode = this.beforeTreeNode;
				}
				else
				{
					this.treeView1.SelectedNode = e.Node.Nodes[0];
				}
				IL_3DF:
				this.beforeTreeNode = this.treeView1.SelectedNode;
				bool flag = false;
				foreach (FittingInfo fittingInfo in this.structList)
				{
					if (fittingInfo.systemClass == this.treeView1.SelectedNode.Name)
					{
						foreach (object obj3 in this.listView1.Items)
						{
							ListViewItem listViewItem3 = (ListViewItem)obj3;
							if (listViewItem3.Text == fittingInfo.fittingName)
							{
								listViewItem3.Selected = true;
								flag = true;
								break;
							}
						}
					}
				}
				if (!flag)
				{
					this.listView1.Items[0].Selected = true;
				}
				this.isLoad = true;
			}
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
                this.treeView1.SelectedNode.BackColor = System.Drawing.Color.White;
                this.treeView1.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void FamilyLoad()
		{
			DbFamily family = this.treeView1.SelectedNode.Tag as DbFamily;
			DbFamilySymbol firstFamilySymbol = this.hvacDR.GetFirstFamilySymbol(family);
			if (firstFamilySymbol != null)
			{
				this.fs = firstFamilySymbol.Create(this.doc);
				if (this.fs == null)
				{
					throw new Exception("加载族失败");
				}
			}
			else
			{
				MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void setUserSeleect(string mmm, string nodeName, ref DbFamily df)
		{
			if (this.tnSelected.Text == nodeName && this.tnSelected.Parent.Text == mmm)
			{
				foreach (object obj in this.listView1.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					if (listViewItem.Selected)
					{
						df = (listViewItem.Tag as DbFamily);
					}
					else
					{
                        listViewItem.BackColor = System.Drawing.Color.White;
                        listViewItem.ForeColor = System.Drawing.Color.Black;
					}
				}
			}
		}

		private void listView1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.listView1.SelectedItems.Count == 1)
			{
                this.tnSelected.BackColor = System.Drawing.Color.DodgerBlue;
                this.tnSelected.ForeColor = System.Drawing.Color.White;
				if (this.message == "风对齐")
				{
					if (this.isload2 && this.isload1 && this.treeView1.SelectedNode.Level == 2)
					{
						this.setUserSeleect("矩形三通", "顶对齐", ref this.teeTopFamily);
						this.setUserSeleect("矩形三通", "底对齐", ref this.teebotFamily);
						this.setUserSeleect("矩形四通", "顶对齐", ref this.crossTopFamily);
						this.setUserSeleect("矩形四通", "底对齐", ref this.crossbotFamily);
						this.setUserSeleect("矩形三通", "中心对齐", ref this.teeMiddleFamily);
						this.setUserSeleect("矩形四通", "中心对齐", ref this.crossMiddleFamily);
						return;
					}
				}
				else if (this.isLoad)
				{
					bool flag = false;
					if (this.structList != null)
					{
						foreach (FittingInfo fittingInfo in this.structList)
						{
							if (fittingInfo.systemClass == this.treeView1.SelectedNode.Name)
							{
								IEnumerator enumerator2 = this.listView1.Items.GetEnumerator();
								{
									while (enumerator2.MoveNext())
									{
										object obj = enumerator2.Current;
										ListViewItem listViewItem = (ListViewItem)obj;
										if (listViewItem.Selected)
										{
											fittingInfo.fittingName = listViewItem.Text;
											MessageBox.Show(listViewItem.Text);
											flag = true;
											break;
										}
									}
									break;
								}
							}
						}
					}
					if (!flag)
					{
						FittingInfo fittingInfo2 = new FittingInfo();
						fittingInfo2.fittingName = this.listView1.Items[0].Text;
						fittingInfo2.systemClass = this.treeView1.SelectedNode.Name;
						this.structList.Add(fittingInfo2);
						MessageBox.Show(this.listView1.Items[0].Text + "111");
					}
				}
			}
		}

		private List<DbFamily> teeTop = new List<DbFamily>();

		private List<DbFamily> teebot = new List<DbFamily>();

		private List<DbFamily> crossTop = new List<DbFamily>();

		private List<DbFamily> crossbot = new List<DbFamily>();

		private List<DbFamily> teeMiddle = new List<DbFamily>();

		private List<DbFamily> crossMiddle = new List<DbFamily>();

		private TreeNode tnSelected;

		private DbFamily teeTopFamily;

		private DbFamily teebotFamily;

		private DbFamily crossTopFamily;

		private DbFamily crossbotFamily;

		private DbFamily teeMiddleFamily;

		private DbFamily crossMiddleFamily;

		private HVACDataReader hvacDR;

		private Document doc;

		private string message = "";

		public FamilySymbol fs;

		private List<FittingInfo> structList = new List<FittingInfo>();

		private string nodeName = "";

		private HVACUserSetting userSetting = new HVACUserSetting();

		private bool isload1;

		private bool isload2;

		private bool isLoad;

		private TreeNode beforeTreeNode;
	}
}
